package com.xk857.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xk857.entities.SysUser;
import com.xk857.system.req.SysUserCheckPasswordREQ;
import com.xk857.system.req.SysUserREQ;
import com.xk857.system.req.SysUserUpdatePasswordREQ;
import com.xk857.system.service.ISysUserService;
import com.xk857.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息表 前端控制器
 *
 * @author CV大魔王
 * @since 2021-02-25
 */
@Api(value = "用户管理接口", tags = "用户管理接口")
@RestController
@RequestMapping("/pri/user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;


    @ApiOperation("根据用户名和手机号查询用户列表接口")
    @PostMapping("/search")
    public IPage<SysUser> search(@RequestBody SysUserREQ req) {
        return sysUserService.queryPage(req);
    }


    @ApiImplicitParam(name = "id", value = "用户Id", required = true)
    @ApiOperation("根据用户id查询所拥有的角色ids接口")
    @GetMapping("/{id}/role/ids")
    public List<String> findRoleIdsById(@PathVariable("id") String id) {
        return sysUserService.findRoleIdsById(id);
    }


    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户Id", required = true), @ApiImplicitParam(name = "roleIds", value = "角色Id集合", required = true, allowMultiple = true, dataType = "String"),})
    @ApiOperation("新增用户角色关系数据接口")
    @PostMapping("/{id}/role/save")
    public Result saveUserRole(@PathVariable("id") String id, @RequestBody List<String> roleIds) {
        return sysUserService.saveUserRole(id, roleIds);
    }

    @ApiOperation("通过用户ID删除用户接口")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        return sysUserService.deleteById(id);
    }


    @ApiOperation("新增用户信息接口")
    @PostMapping // 请求地址 /user
    public Result save(@RequestBody SysUser sysUser) {
        // 密码加密处理
        sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
        // 新增
        sysUserService.save(sysUser);
        return Result.ok();
    }


    @ApiOperation("查询用户详情接口")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true)
    @GetMapping("/{id}")
    public SysUser view(@PathVariable("id") String id) {
        return sysUserService.getById(id);
    }


    @ApiOperation("校验原密码接口")
    @PostMapping("/check/password")
    public Result checkPassword(@RequestBody SysUserCheckPasswordREQ req) {
        return sysUserService.checkPassword(req);
    }


    @ApiOperation("更新用户密码接口")
    @PutMapping("/password")
    public Result updatePassword(@RequestBody SysUserUpdatePasswordREQ req) {
        return sysUserService.updatePassword(req);
    }


    @ApiOperation("更新用户信息接口")
    @PutMapping
    public Result update(@RequestBody SysUser sysUser) {
        return sysUserService.update(sysUser);
    }


    @ApiOperation("统计总用户数接口")
    @GetMapping("/total")
    public Integer userTotal() {
        return sysUserService.getUserTotal();
    }


}
